package com.chidopi.market;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

@Configuration
@ImportResource("classpath:resources.xml")
public class RootConfig {
	private static Logger logger = LoggerFactory.getLogger(RootConfig.class);
	private @Value("#{jdbcProperties.driverClassName}") String driverClass;
	private @Value("#{jdbcProperties.url}") String jdbcUrl;
	private @Value("#{jdbcProperties.username}") String username;
	private @Value("#{jdbcProperties.password}") String password;
	private @Value("#{appProperties.bookcity_res_path}") String BOOKCITY_RES_PATH;

	@Bean
	public DataSource dataSource() {
		DriverManagerDataSource dataSource = new DriverManagerDataSource();
		dataSource.setDriverClassName(this.driverClass);
		dataSource.setUrl(this.jdbcUrl);
		dataSource.setUsername(this.username);
		dataSource.setPassword(this.password);
		logger.debug("init dataSource");
		return dataSource;
	}
	
    public String getBookcityResPath(){
    	if(BOOKCITY_RES_PATH.endsWith("/"))
			return BOOKCITY_RES_PATH;
		else
            return BOOKCITY_RES_PATH + "/";	
    }
}
